import 'package:flutter/material.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:audio_wave/audio_wave.dart';

const double _boxHeight = 40.0;

const double _waveWidth = 110.0;
const double _waveHeight = 20.0;
const int _waveMaxCount = 21;

class AudioWaveWidget extends StatelessWidget {
  final bool isPlaying;
  // 只是为了展示 具体动态数字请用 durationChild
  final int duration;
  // 动态秒数 防止组件反复重建
  final Widget? durationChild;
  final VoidCallback? onTap;

  const AudioWaveWidget({
    Key? key,
    this.isPlaying = false,
    this.duration = 0,
    this.durationChild,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<AudioWaveBar> bars = [];
    double waveWidth = 0;
    if (duration <= 3) {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.5, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.5, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth / _waveMaxCount * 3;
    } else if (duration <= 6) {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth / _waveMaxCount * 5;
    } else if (duration <= 12) {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth / _waveMaxCount * 8;
    } else if (duration <= 18) {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth / _waveMaxCount * 11;
    } else if (duration <= 30) {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth / _waveMaxCount * 16;
    } else {
      bars.addAll([
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.4, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 1, color: AppColors.unactive4),
        AudioWaveBar(heightFactor: 0.6, color: AppColors.unactive4),
      ]);
      waveWidth = _waveWidth;
    }
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: waveWidth + 80,
        height: _boxHeight,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(_boxHeight),
          border: Border.all(color: AppColors.border, width: 2),
          color: AppColors.page,
        ),
        child: Row(
          children: [
            _buildPlayIcon(),
            _buildWave(bars, waveWidth),
            _buildDurationText(),
          ],
        ),
      ),
    );
  }

  Widget _buildWave(List<AudioWaveBar> bars, waveWidth) {
    if (isPlaying) {
      return AudioWave(
        height: _waveHeight,
        width: waveWidth,
        spacing: 3.5,
        animation: true,
        bars: bars,
        key: UniqueKey(),
      );
    }
    return AudioWave(
      height: _waveHeight,
      width: waveWidth,
      spacing: 3.5,
      animation: false,
      animationLoop: 0,
      bars: bars,
      key: UniqueKey(),
    );
  }

  Widget _buildDurationText() {
    return Expanded(
      child: durationChild ?? AudioWaveDurationText('${duration}s'),
    );
  }

  Widget _buildPlayIcon() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 6),
      child: Icon(
        isPlaying ? Icons.pause_rounded : Icons.play_arrow_rounded,
        size: 24,
        color: AppColors.active2,
      ),
    );
  }
}

class AudioWaveDurationText extends StatelessWidget {
  final String value;
  const AudioWaveDurationText(this.value, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      value,
      style: const TextStyle(
        fontSize: 12,
        color: AppColors.active2,
      ),
      textAlign: TextAlign.center,
    );
  }
}
